﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CardPlay.Client.Model.Persistence
{
    public class LocalStorage : IStorage
    {
        public static Player CurrentPlayer
        {
            get
            {
                var id = new Guid("{21353869-E4B1-4451-9169-D402E0620BE4}");

                var context = new DataContext();
                var player = context.Players.Find(id);

                if (player == null)
                {
                    player = new Player
                             {
                                 Id = id,
                                 DisplayName = "John Smith"
                             };

                    context.Players.Add(player);
                    context.SaveChanges();
                }

                return player;
            }
        }

        public void AugmentHistory(RecordOfPlay record)
        {
            var context = new DataContext();

            GamePlay play = record;
            play.Player = context.Players.Find(CurrentPlayer.Id);

            context.History.Add(play);
            context.SaveChanges();
        }

        public IEnumerable<RecordOfPlay> HistoryOfPlayForGame(Guid gameId)
        {
            var context = new DataContext();
            return context.History.Where(play => play.IdOfGame == gameId).ToArray().Select(play => play.ToRecordOfPlay());
        }

        //public IEnumerable<RecordOfPlay> HistoryOfPlayForGameByPlayer(Guid gameId, Guid playerId)
        //{
        //    var context = new DataContext();
        //    return context.History.Local.Where(play => play.IdOfGame == gameId && play.Player.Id == playerId).Select(play => play.ToRecordOfPlay());
        //}

        //public IEnumerable<RecordOfPlay> HistoryOfPlayByPlayer(Guid playerId)
        //{
        //    var context = new DataContext();
        //    return context.History.Local.Where(play => play.Player.Id == playerId).Select(play => play.ToRecordOfPlay());
        //}
    }
}